package com.hc.conferencecheck.service.impl;

import com.hc.conferencecheck.entity.ResultMsg;
import com.hc.conferencecheck.entity.entityDTO.ConventionerAttendRecord;
import com.hc.conferencecheck.mapper.AttendRecordMapper;
import com.hc.conferencecheck.service.AttendRecordService;
import com.hc.conferencecheck.service.ExportExcelService;
import com.hc.conferencecheck.utils.ExportExcelUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用于导出excel的service实现类
 */
//开启事务管理
@Transactional
@Service
public class ExportExcelServiceImpl implements ExportExcelService {
    @Resource
    private AttendRecordMapper attendRecordMapper;

    /**
     * 导出用户会议申请记录
     */
    public void exportConventionerAttendRecords(Map<String, Object> params, HttpServletResponse response) {
        //根据条件查询会议申请记录
        List<ConventionerAttendRecord> dataList = attendRecordMapper.selectConventionerAttendRecord(params);
        //拼接标题
        String title = "用户会议申请记录";
        //创建列名集合
        List<String> rowName = new ArrayList<>();
        rowName.add("序号");
        rowName.add("申请记录ID");
        rowName.add("会议ID");
        rowName.add("会议名称");
        rowName.add("用户ID");
        rowName.add("用户名称");
        rowName.add("用户手机号");
        rowName.add("申请时间");
        rowName.add("是否签到");
        rowName.add("签到时间");
        rowName.add("是否支付");
        rowName.add("支付账号");
        rowName.add("支付金额");
        rowName.add("审核是否通过");
        rowName.add("审核时间");

        //调用导出方法，进行文件导出

        if (dataList == null || dataList.size() == 0) {
            try {
                // excel 表文件名
                String fileName = title + ".csv";
                fileName = "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1");
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                response.setHeader("Content-Disposition", fileName);
                OutputStream out = response.getOutputStream();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //导出数据
        if (dataList != null && dataList.size() > 0) {
            ExportExcelUtils exportExcelUtils = new ExportExcelUtils(title, rowName, dataList, response);
            exportExcelUtils.exportConventionerAttendRecords();
        }
    }

}
